# -*-coding:utf-8-*-
import time

from selenium import webdriver
import sys

from selenium.common.exceptions import NoSuchElementException

from config.globalconfig import config_driver
import time
from selenium.webdriver.support.select import Select

from public.common.add_cookie import AddCookie


class Page(object):

    def __init__(self, url):
        self.url = url
        if config_driver == "firefox" or config_driver == "ff":
            self.dirver = webdriver.Firefox()
        elif config_driver == "chrome" or config_driver == "Chrome":
            self.dirver = webdriver.Chrome()
        elif config_driver == "internet explorer" or config_driver == "ie":
            self.dirver = webdriver.Ie()
        elif config_driver == "opera":
            self.dirver = webdriver.Opera()
        elif config_driver == "phantomjs":
            self.dirver = webdriver.PhantomJS()
        elif config_driver == "edge":
            self.dirver = webdriver.Edge()
        else:
            print("please input 'xxx'" + sys.argv[0])

        if not self.url:
            print("xx" + sys.argv[0])

    def open(self):
        try:
            self.dirver.implicitly_wait(10)
            self.dirver.get(url=self.url)
        except:
            print("打开{0}失败:{1}".format(self.url, sys.argv[0]))

    def script(self, scr):
        self.dirver.implicitly_wait(10)
        try:
            self.dirver.execute_script(scr)
        except:
            js = None
            print("java_script定位元素{0}失败:{1}".format(scr, sys.argv[0]))

    def get_element(self, *loc):
        self.dirver.implicitly_wait(10)
        if len(loc) != 2:
            # TODO
            pass
        elif loc[0] not in ("id", "name", "class name", "text", "xpath"):
            # TODO
            raise NameError("{} is not 'id' or 'name' or 'class name' 'text' 'xpath' ".format(loc[0]))
        try:
            el = self.dirver.find_element(*loc)
            return el
        except:
            raise

    def get_elments(self, *loc):
        self.dirver.implicitly_wait(10)
        if len(loc) != 2:
            # TODO
            pass
        elif loc[0] not in ("id", "name", "class name", "text", "xpath"):
            # TODO
            raise NameError("{} is not 'id' or 'name' or 'name class' 'text' 'xpath' ".format(loc[0]))
        try:
            els = self.dirver.find_elements(*loc)
            return els
        except:
            raise

    def clear_text(self, *loc):
        try:
            self.dirver.find_element(*loc).clear()
        except:
            pass

    def get_alter(self):
        try:
            self.dirver.switch_to.alert()
        except:
            pass

    def get_alter_text(self):
        try:
            text = self.dirver.switch_to.alert.text
        except:
            return
        return text

    def save_picture(self, filename, func):
        now = time.strftime("%Y-%m-%d_%H_%M_%S")
        self.dirver.save_screenshot(filename + "\\" + func + now + ".png")

    def text_clear(self, *loc):
        try:
            self.get_element(*loc).clear()
        except:
            pass

    def get_text(self, *loc):
        text = self.get_element(*loc).text
        return text

    def attribute(self, *loc, name):
        try:
            el = self.get_element(*loc)
            attr = el.get_attribute(name)
            return attr
        except:
            print("未获取到{0}标签{1}属性".format(loc[1], name))
            raise

    def select(self, name, type, value):
        self.dirver.implicitly_wait(10)
        try:
            s = Select(self.dirver.find_element_by_id(name))
        except:
            return

        if type not in ("index", "value", "text"):
            print("please input 'index' or 'value' or 'text'")
            return
        if type == "index" and isinstance(value, int):
            try:
                s.select_by_index(value)
            except:
                pass
            else:
                return
        else:
            print("value is not int")

        if type == "value" and isinstance(value, str):
            try:
                s.select_by_value(value)
            except:
                pass
            else:
                return
        else:
            print("value is not str")

        if type == "text" and isinstance(value, str):
            try:
                s.select_by_visible_text(value)
            except:
                pass
            else:
                return
        else:
            print("value is not str")

    @property
    def is_cookie(self):
        self.dirver.get(url=self.url)
        try:
            self.get_element(*("class name", "login_header"))
        except NoSuchElementException:
            return True
        else:
            add_cookie = AddCookie(self)
            add_cookie.add_cookie()
            return False


if __name__ == "__main__":
    pass
